package com.cskaoyan.mall.controller.mall;

import com.cskaoyan.mall.model.comment.CommentVO;
import com.cskaoyan.mall.model.orders.AddOrderBO;
import com.cskaoyan.mall.model.orders.CartList;
import com.cskaoyan.mall.model.orders.OrderByStateVO;
import com.cskaoyan.mall.model.orders.SettleAccounts;
import com.cskaoyan.mall.service.OrderService;
import com.cskaoyan.mall.service.OrderServiceImpl;
import com.cskaoyan.mall.util.RequestBodyUtils;
import com.cskaoyan.mall.util.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: jyf2000jyf
 */
@WebServlet("/api/mall/order/*")
public class OrderServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/mall/order/", "");
        if ("settleAccounts".equals(op)) {
            settleAccounts(request, response);
        } else if("addOrder".equals(op)){
            addOrder(request,response);
        } else if("sendComment".equals(op)){
            sendComment(request,response);
        }
    }

    private void settleAccounts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        CartList cartList = gson.fromJson(requestBody, CartList.class);
        // TODO pay
        boolean status = orderService.settleAccounts(cartList);
        Result result;
        if (status) {
            result = new Result(0, "下单成功", null);
        } else {
            result = new Result(10000, "下单失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }


    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        AddOrderBO addOrderBO = gson.fromJson(requestBody, AddOrderBO.class);
        boolean status = orderService.addOrder(addOrderBO);
        Result result;
        if (status) {
            result = new Result(0, "购买/加入购物车成功", null);
        } else {
            result = new Result(10000, "购买/加入购物车失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void sendComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        CommentVO commentVO = gson.fromJson(requestBody, CommentVO.class);
        boolean status = orderService.sendComment(commentVO);
        Result result;
        if (status) {
            result = new Result(0, "评价成功", null);
        } else {
            result = new Result(10000, "评价失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/mall/order/", "");
        if ("getOrderByState".equals(op)) {
            getOrderByState(request, response);
        } else if ("pay".equals(op)) {
            pay(request, response);
        } else if ("deleteOrder".equals(op)) {
            deleteOrder(request, response);
        } else if ("confirmReceive".equals(op)) {
            confirmReceive(request, response);
        }
    }

    private void pay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // TODO pay
        Integer id = Integer.parseInt(request.getParameter("id"));
        boolean status = orderService.pay(id);
        Result result;
        if (status) {
            result = new Result(0, "支付成功", null);
            response.getWriter().println(gson.toJson(result));
            return;
        } else {
            result = new Result(10000, "支付失败", null);
            response.getWriter().println(gson.toJson(result));
        }
    }

    private void getOrderByState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer state = Integer.parseInt(request.getParameter("state"));
        String username = request.getParameter("token");
        List<OrderByStateVO> orderByStateVOList = orderService.getOrderByState(state, username);
        Result result = new Result(0, null, orderByStateVOList);
        response.getWriter().println(gson.toJson(result));
    }

    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean status = orderService.deleteOrder(id);
        Result result;
        if (status) {
            result = new Result(0, "删除成功", null);
        } else {
            result = new Result(10000, "删除失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }


    private void confirmReceive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        boolean status = orderService.confirmReceive(id);
        Result result;
        if (status) {
            result = new Result(0, "收货成功", null);
            response.getWriter().println(gson.toJson(result));
            return;
        } else {
            result = new Result(10000, "收货失败", null);
            response.getWriter().println(gson.toJson(result));
        }
    }
}
